﻿using System.Configuration;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web.Script.Serialization;
using LY.SSOServer.Services;

namespace LY.SSOServer.Models
{
    public class ApiRepository<T>
    {

        public static WebApiMsg Get(string apiUrl)
        {
            var jsonData = new WebApiMsg();
            var client = new HttpClient { BaseAddress = new System.Uri(ConfigurationManager.AppSettings["webApi"]) };
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("appliction/json"));
            HttpResponseMessage response = client.GetAsync(apiUrl).Result;
            if (response.IsSuccessStatusCode)
            {
                var serializer = new JavaScriptSerializer();
                var model = serializer.Deserialize<WebApiMsg>(response.Content.ReadAsStringAsync().Result);
                return model;
            }
            else
            {
                jsonData.status = -1;
                jsonData.message = "调用错误";
            }
            return jsonData;
        }

        public static WebApiMsg<T> Post(string apiUrl, HttpContent content)
        {
            var jsonData = new WebApiMsg<T>();
            var client = new HttpClient { BaseAddress = new System.Uri(ConfigurationManager.AppSettings["webApi"]) };
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/json"));
            HttpResponseMessage response = client.PostAsync(apiUrl, content).Result;
            if (response.IsSuccessStatusCode)
            {
                var serializer = new JavaScriptSerializer();
                var model = serializer.Deserialize<WebApiMsg<T>>(response.Content.ReadAsStringAsync().Result);
                return model;

            }
            return jsonData;
        }

        public static WebApiMsg Put(string apiUrl, HttpContent content)
        {
            var jsonData = new WebApiMsg();
            var client = new HttpClient { BaseAddress = new System.Uri(ConfigurationManager.AppSettings["webApi"]) };
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("appliction/json"));
            HttpResponseMessage response = client.PutAsync(apiUrl, content).Result;
            if (response.IsSuccessStatusCode)
            {
                var serializer = new JavaScriptSerializer();
                var model = serializer.Deserialize<WebApiMsg>(response.Content.ReadAsStringAsync().Result);
                return model;
            }
            else
            {
                jsonData.status = -1;
                jsonData.message = "调用错误";
            }
            return jsonData;
        }
        public static WebApiMsg Delete(string apiUrl)
        {
            var jsonData = new WebApiMsg();
            var client = new HttpClient { BaseAddress = new System.Uri(ConfigurationManager.AppSettings["webApi"]) };
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("appliction/json"));
            HttpResponseMessage response = client.DeleteAsync(apiUrl).Result;
            if (response.IsSuccessStatusCode)
            {
                var serializer = new JavaScriptSerializer();
                var model = serializer.Deserialize<WebApiMsg>(response.Content.ReadAsStringAsync().Result);
                return model;
            }
            else
            {
                jsonData.status = -1;
                jsonData.message = "调用错误";
            }
            return jsonData;
        }


    }
}